﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading.Tasks;

namespace TMOOC.CM.MobileShop.Util
{
    public static class MLogHelper
    {
        private static string logPath = string.Empty;
        /// <summary>
        /// 保存日志的文件夹
        /// </summary>
        public static string LogPath
        {
            get
            {
                //配置默认目录
                if (logPath == string.Empty)
                    logPath = AppDomain.CurrentDomain.BaseDirectory + @"\Log\";
                //目录不存在则新建
                if (!Directory.Exists(logPath))
                    Directory.CreateDirectory(logPath);
                return logPath;
            }
            set { logPath = value; }
        }

        private static string logFielPrefix = string.Empty;
        /// <summary>
        /// 日志文件前缀
        /// </summary>
        public static string LogFielPrefix
        {
            get { return logFielPrefix; }
            set { logFielPrefix = value; }
        }

        /// <summary>
        /// 写日志
        /// </summary>
        public static void WriteLog(string logFile, string msg, string logFilePrefix = "")
        {
            try
            {
                System.IO.StreamWriter sw = System.IO.File.AppendText(
                    LogPath + (string.IsNullOrEmpty(LogFielPrefix) ? logFilePrefix : LogFielPrefix) + logFile + " " +
                    DateTime.Now.ToString("yyyyMMdd") + ".Log"
                    );
                sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss: ") + msg);
                sw.Close();
                Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss: ") + msg);
            }
            catch
            { }
        }

        public  static void Info(string msg, string logFilePrefix = "", bool isWrite = true)
        {
            if (isWrite)
            {
                WriteLog(LogFile.Info.ToString(), msg, logFilePrefix);
            }
        }

        public static void Error(string msg, string logFilePrefix = "", bool isWrite = true)
        {
            if (isWrite)
            {
                WriteLog(LogFile.Error.ToString(), msg, logFilePrefix);
            }
        }

        public static void Warning(string msg, string logFilePrefix = "", bool isWrite = true)
        {
            if (isWrite)
            {
                WriteLog(LogFile.Warning.ToString(), msg, logFilePrefix);
            }
        }
        public static void SQL(string msg, string logFilePrefix = "", bool isWrite = true)
        {
            if (isWrite)
            {
                WriteLog(LogFile.SQL.ToString(), msg, logFilePrefix);
            }
        }
    }

    /// <summary>
    /// 日志类型
    /// </summary>
    public enum LogFile
    {
        Info,
        Warning,
        Error,
        SQL
    }
}
